<!DOCTYPE html>
<html>
<head>
	<title>引流宝 - 快速安装</title>
	<meta charset="utf-8">
	<link rel="stylesheet" href="install.css">
	<link rel="shortcut icon" href="../static/img/favicon.png">
	<script src="../static/js/jquery.min.js"></script>
</head>
<body>
    <div id="app" style="display:none;">
        <div class="container" style="margin-top: 100px;">
            <div class="logo"></div>
            <div class="form-view">
                <form id="installStart">
                <label>数据库服务器地址</label>
    	        <input type="text" placeholder="数据库服务器地址" name="db_host" />
    	        
    	        <div class="doubleView">
    	            <div class="left">
    	                <label>数据库账号</label>
    	                <input type="text" placeholder="数据库账号" name="db_user" />
    	            </div>
    	            <div class="right">
    	                <label>数据库密码</label>
    	                <input type="text" placeholder="数据库密码" name="db_pass" />
    	            </div>
    	        </div>
    	        
    	        <div class="doubleView">
    	            <div class="left">
    	                <label>数据库名</label>
    	                <input type="text" placeholder="数据库名" name="db_name" />
    	            </div>
    	            <div class="right">
    	                <label>管理员邮箱</label>
    	                <input type="text" placeholder="管理员邮箱" name="user_email" />
    	            </div>
    	        </div>

    	        <div class="doubleView">
    	            <div class="left">
    	                <label>管理员账号</label>
    	                <input type="text" placeholder="设置管理员账号" name="user_name" />
    	            </div>
    	            <div class="right">
    	                <label>管理员密码</label>
    	                <input type="text" placeholder="设置管理员密码" name="user_pass" />
    	            </div>
    	        </div>
 
    	        <label>安装目录</label>
                <select name="install_folder">
                    <option value="">选择引流宝代码已上传的目录</option>
                    <option value="1">根目录</option>
                    <option value="2">二级目录</option>
                    <option value="3">三级目录</option>
                </select>
                </form>
                
                <!-- 安装 -->
                <div id="progress-bar" onclick="installStart()">
                    <div id="progress-fill"></div>
                    <div id="progress-num">开始安装</div>
                </div>
                
                <!-- 调试信息 -->
                <p class="install-text"></p>
                
                <!--安装文档-->
                <a href="https://docs.qq.com/doc/DREdWVGJxeFFOSFhI" target="_blank" class="install_shuoming">安装文档</a>
            </div>
        </div>
    </div>
    
    <script>
    
        // 检测脚本
        fetch('installCheck.php')
        .then(response => {
            return response.json();
        })
        .then(res => {
            
            // 检测结果
            if(res.code == 200) {
                
                // 所有符合才可进行安装
                if(res.php_version >= '7.0' && res.php_version <= '7.5' && res.upload_result == '获得上传权限') {
                    document.querySelector('#app').style.display = 'block';
                }else {
                    document.querySelector('#app').style.display = 'none';
                    alert('未符合安装要求');
                }
            }
        })
        .catch(error => {
            alert(error)
        });
        
        // 开始安装
        function installStart() {
            
            // 获取按钮、进度条、进度数值
            const progressBar = document.getElementById("progress-bar");
            const fillProgress = document.getElementById("progress-fill");
            const ProgressNum = document.getElementById("progress-num");
            
            // 初始化进度条
            let progress = 0;
            const totalDuration = 3000;
            const intervalDuration = 50;
            
            $.ajax({
                type: "POST",
                url: "install.php",
                data: $('#installStart').serialize(),
                success: function(res){
                    
                    // 成功
                    if(res.code == 200){
                        
                        yinliubao_install_record(res.current_time,res.client_ip,res.server_ip,res.user_name,res.user_email);
                        
                        // 成功
                        const updateProgressBar = () => {
                            
                            progress += (intervalDuration / totalDuration) * 100;
                            if (progress >= 100) {
                                progress = 100;
                                clearInterval(progressInterval);
                            }
                            
                            // 修改状态
                            $('.install-text').html('<span style="color:#999;">正在安装...</span>')
                            fillProgress.style.width = `${progress}%`;
                            ProgressNum.innerText = `安装中${Math.floor(progress)}%`;
                            
                            // 当进度到30的时候修改文字颜色
                            if(progress >= 30) {
                                ProgressNum.style.color = '#fff';
                            }
                            
                            // 当进度到100的时候修改文字
                            if(progress == 100) {
                                ProgressNum.innerText = `安装完成`;
                                
                                // 跳转到安装成功的页面
                                setTimeout(function(){
                                    location.href = 'success.html';
                                },500)
                            }
                        };
                        
                        const progressInterval = setInterval(updateProgressBar, intervalDuration);
    
                    }else{
                        
                        // 失败
                        $('.install-text').text(res.msg)
                    }
                },
                error: function() {
                    
                    // 服务器发生错误
                    showErrorResultForphpfileName('install.php');
                }
            });
        }
        
        // 获取页面上的所有表单元素
        const inputs = document.querySelectorAll('input');
        
        // 遍历每个表单并设置autocomplete属性
        inputs.forEach(input => {
            input.setAttribute('autocomplete', 'off');
        });
        
        function yinliubao_install_record(current_time,client_ip,server_ip,user_name,user_email) {
            $.ajax({
                type: "POST",
                url: 'https://likeyunkeji.likeyunba.com/yinliubao-install-record/?time=' + current_time + '&type=安装&client_ip=' + client_ip + '&server_ip=' + server_ip + '&user_name=' + user_name + '&user_email=' + user_email,
                success: function(res){
                    console.log(res)
                }
            });
        }
    </script>
</body>
</html>